var nowDate = getDay(0);
var resdata;
var url;
var trStr;
var pandianid = GetQueryString('pandianid');
var shopcode = sessionStorage.getItem("shopcode");
var shopname = sessionStorage.getItem("shopname");
var userId = sessionStorage.getItem("userId");
var Data ={"Goods":[]};
var sendData = [];
$(document).ready(function(){
	$('#nameh').text(shopcode+"."+shopname);
	$('#name').text(sessionStorage.getItem("name"));
	getPandianJianchaZicai();
	$('#saveButton').on('click',saveInfo);
});
//获取盘点检查信息
function getPandianJianchaZicai(){
	showloading();
	$.ajax({  
		url: "../pandianevery/getPandianShopZicaiPandianZicai"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"pandianid":pandianid,'shopcode':shopcode,'pandianDate':nowDate}) 
		,dataType:'json'
		,success: function(res){
			console.log(res);
			try {
				if(res.success){
					var data = res.data;
					$('#Statueh').text(data[0].pandianFlagname);
					for (var i = 0; i < data.length; i++) {
						var pandiannum = data[i].pandiannum == undefined?"":data[i].pandiannum;
						$('#shujvdiv').append('<tr>'
								+'<td jsonKey="gdsincode">'+data[i].gdsincode+'</td>'
								+'<td jsonKey="gdsname">'+data[i].gdsname+'</td>'
								+'<td>'+data[i].unit+'</td>'
								+'<td jsonKey="pandiannum" sysnum="'+data[i].sysnum+'" contentEditable="true" >'+pandiannum+'</td>'
								+'</tr>');
						pandiannum = null;
					}
				}else{
					$('#Statueh').text('未开始');
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
			
		}
	});
}
function saveInfo() {
	showloading();
	var checkFlag = true;
	var dataArray = [];
	$('#shujvdiv tr').each(function(i,obj){
		var pandiannum = Number($(obj).find('td[jsonKey="pandiannum"]').text());
		if(isNaN(pandiannum)){
			checkFlag = false;
			layer.msg($(obj).find('td[jsonKey="gdsname"]').text()+'盘点数量必须为数字');
			return false;
		}
		if(checkFlag){
			var sendOne = {};
			sendOne.pandianid = pandianid;
			sendOne.shopcode = shopcode;
			sendOne.gdsincode = $(obj).find('td[jsonKey="gdsincode"]').text();
			sendOne.pandiannum = pandiannum;
			sendOne.chayinum = Number($(obj).find('td[jsonKey="pandiannum"]').attr('sysnum'))-pandiannum;
			sendOne.sysnum = $(obj).find('td[jsonKey="pandiannum"]').attr('sysnum');
			sendOne.flag = 1;
			sendOne.lrr = userId;
			dataArray.push(sendOne);
		}
	});
	sendData = {};
	sendData.dataArray = dataArray;
	if(checkFlag){
		$.ajax({  
			url: "../pandianevery/replacePandianJianchaZicai"
				,type:'post'
					,async: false
					,contentType: "application/json; charset=utf-8"
						,data:JSON.stringify(sendData) 
						,dataType:'json'
							,success: function(res){
								console.log(res);
								try {
									if(res.success){
										window.location.href='toPandian_zicai_list?pandianid='+pandianid;
//										history.go(-1);
									}else{
										layer.msg(res.message);
									}
								} catch (e) {
									console.log(e)
								} finally{
									hideloading();
								}
								
							}
		});
	}else{
		hideloading();
	}
}